package com.leyou.item.controller;

import com.leyou.item.dto.*;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     *分页查询spu
     * @param brandId
     * @param categoryId
     * @param id
     * @param page
     * @param rows
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageSpuDTO> goodsPage(
           @RequestParam(value = "brandId",required = false) Long brandId,
           @RequestParam(value = "categoryId",required = false) Long categoryId,
           @RequestParam(value = "id",required = false) Long id,
           @RequestParam(value = "page",required = false,defaultValue = "1") Long page,
           @RequestParam(value = "rows",required = false,defaultValue = "5") Long rows,
           @RequestParam(value = "saleable",required = false) Boolean saleable
    )
    {
        return ResponseEntity.ok(goodsService.goodsPage(brandId,categoryId,id,page,rows,saleable));
    }


    /**
     *新增商品
     * @param spuDTO 商品DTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> insertGood(@RequestBody SpuDTO spuDTO)
    {
        goodsService.insertGood(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品表
     * @param spuDTO 商品DTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateGoog(@RequestBody SpuDTO spuDTO)
    {
        goodsService.updateGoog(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据id集合查询sku集合
     * @param ids sku id集合
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> selectSkusByIds(@RequestParam("ids") List<Long> ids)
    {
        return ResponseEntity.ok(goodsService.selectSkusByIds(ids));
    }

    /**
     * 根据spuid查询sku集合
     * @param sid
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> selectSkusById(@RequestParam("id") Long sid)
    {
        return ResponseEntity.ok(goodsService.selectSkusById(sid));
    }

    /**
     *
     *
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> selectSpuDetailById(@RequestParam("id") Long id)
    {
        return ResponseEntity.ok(goodsService.selectSpuDetailById(id));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> selectSpecValueById(@RequestParam("id") Long id,@RequestParam(value = "searching",required = false) Boolean searching)
    {
        return ResponseEntity.ok(goodsService.selectSpecValueById(id,searching));
    }

    /**
     * 修改商品上下架
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleableById(@RequestParam(value = "id",required = false) Long id,@RequestParam(value = "saleable",required = false) Boolean saleable)
    {
        goodsService.updateSpuSaleableById(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询spu及sku、spuDetail
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> selectSpuSkuSpudetailById(@PathVariable("id")Long id)
    {
        return ResponseEntity.ok(goodsService.selectSpuSkuSpudetailById(id));
    }

    /**
     *根据id查询商品
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> selectSpuById(@PathVariable("id")Long id)
    {
       return ResponseEntity.ok(goodsService.selectSpuById(id));
    }

}
